package com.jt.www.enums.extend;

import com.google.common.collect.Maps;
import com.jt.www.enums.endorse.EduSystemEnum;
import com.jt.www.model.remote.ins.InsExtendVO;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Lists;

import java.util.List;
import java.util.Map;

/**
 * 扩展字段查询与赋值对应枚举
 */
public enum ExtendEnum {

    EDU_INSURED_STUDENT_NUM("00", true, true, "S_O_000021", ExtendMothedEnum.INS_OBJECT_EXTENDS, true, "投保学生数") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            if (null != fieldValue) {
                insExtendVO.setInsuredStudentNum(fieldValue);
            }
        }
    },
    EDU_INSURED_NUM("00", true, true, "S_O_000027", ExtendMothedEnum.INS_OBJECT_EXTENDS, true, "投保人数") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            /**学平险使用该字段，列表中放到投保学生数中
             //需要注意其他险种中不能保存该字段，否则会影响到其他查询
             //解决方案1，把该字段的查询优先及调高，单独查询处理，之后由S_O_000021 覆盖该结果
             //解决方案2，插入前判断是否有值，没有插入，有则丢弃
             //解决方案3，加一个独立的字段
             */
            //当前采用方案2
            if (null == insExtendVO.getInsuredStudentNum() && null != fieldValue) {
                insExtendVO.setInsuredStudentNum(fieldValue);
            }
        }
    },
    EDU_INSURED_TEACHER_NUMBER("00", true, true, "S_O_000023", ExtendMothedEnum.INS_OBJECT_EXTENDS, true, "附加教师派遣人数") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            if (null != fieldValue) {
                insExtendVO.setInsuredTeacherNum(fieldValue);
            }
        }
    },
    EDU_INSURED_TEACHER_NUM("00", true, true, "S_O_000046", ExtendMothedEnum.INS_OBJECT_EXTENDS, true, "投保教师人数") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            if (null != fieldValue) {
                insExtendVO.setInsuredTeacherNum(fieldValue);
            }
        }
    },
    EDU_UNIT_TYPE("00", false, false, "S_I_000123", ExtendMothedEnum.CORPORATION_EXTENDS, false, "机构类型") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            if (null != fieldValue) {
                insExtendVO.setUnitTypeDesc(UnitTypeEnum.getMsgByCode(fieldValue));
            }
        }
    },
    EDU_SYSTEM("00", false, false, "S_I_000090", ExtendMothedEnum.CORPORATION_EXTENDS, false, "学制") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            if (null != fieldValue) {
                insExtendVO.setEduSystemDesc(EduSystemEnum.getDescByMsg(fieldValue));
            }
        }
    },
    EDU_SCHOOL_KIND("00", false, false, "S_I_000089", ExtendMothedEnum.CORPORATION_EXTENDS, false, "学校类型") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            if (null != fieldValue) {
                insExtendVO.setSchoolTypeDesc(SchoolTypeEnum.getMsgByCode(fieldValue));
            }
        }
    },
    WXB_SCHOOL_NAME("02", true, false, "S_I_000156", ExtendMothedEnum.HUMAN_EXTENDS, false, "学校名称") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setSchoolName(fieldValue);
        }
    },
    WXB_GRADE("02", true, false, "S_I_000199", ExtendMothedEnum.HUMAN_EXTENDS, false, "年级名称") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setGrade(fieldValue);
        }
    },
    WXB_CLASS_AND_GRADE("02", true, false, "S_I_000198", ExtendMothedEnum.HUMAN_EXTENDS, false, "班级名称") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setClassAndGrade(fieldValue);
        }
    },
    WXB_OFFLINE_SCHOOL_NAME("02,0001", true, false, "S_I_000156", ExtendMothedEnum.HUMAN_EXTENDS, false, "学校名称") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setSchoolName(fieldValue);
        }
    },
    WXB_OFFLINE_GRADE("02,0001", true, false, "S_I_000199", ExtendMothedEnum.HUMAN_EXTENDS, false, "年级名称") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setGrade(fieldValue);
        }
    },
    WXB_OFFLINE_CLASS_AND_GRADE("02,0001", true, false, "S_I_000198", ExtendMothedEnum.HUMAN_EXTENDS, false, "班级名称") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setClassAndGrade(fieldValue);
        }
    },
    INVOICE_RES("", true, false, "P001", ExtendMothedEnum.APP_BIZ_RES, false, "电子发票") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setElectronicInvoice(fieldValue);
            //有电子发票
            insExtendVO.setIsHasElectronicInvoice("是");
        }
    },
    BIZ_RES("", true, false, "A002", ExtendMothedEnum.POLICY_BIZ_RES, false, "保单影印件/电子保单") {
        @Override
        public void setValue(InsExtendVO insExtendVO, String fieldValue) {
            insExtendVO.setElectronicInsurance(fieldValue);
            //有保单影印件/电子保单
            insExtendVO.setIsHasElectronicInsurance("是");
        }
    };


    /**
     * 渠道类型 00：教育 02：我学保
     * 如果channelType配置时为空则支持所有的渠道类型
     */
    private String channelType;

    /**
     * 查询和导出都需要的扩展字段？  true是  false 否
     */
    private boolean queryAndDown;

    /**
     * 只是管理端和业务查询?  true是  false 否（只有管理端）
     */
    private boolean isManagerAndBusiness;

    /**
     * 扩展字段编码
     */
    private String fieldCode;
    /**
     * 请求方法名称
     */
    private ExtendMothedEnum extendMothedEnum;
    /**
     * 查询出来相同的扩展字段名时是否需要累加
     * 用于投保时多个标的扩展字段组时
     * 例如实责，校责 上传多个清单 投保人数，附加险人数累加
     */
    private Boolean isAdd;
    /**
     * 描述
     */
    private String desc;

    ExtendEnum(String channelType, boolean queryAndDown, boolean isManagerAndBusiness, String fieldCode, ExtendMothedEnum extendMothedEnum, Boolean isAdd, String desc) {
        this.channelType = channelType;
        this.queryAndDown = queryAndDown;
        this.isManagerAndBusiness = isManagerAndBusiness;
        this.fieldCode = fieldCode;
        this.extendMothedEnum = extendMothedEnum;
        this.isAdd = isAdd;
        this.desc = desc;
    }

    /**
     * 查询渠道类型下面的扩展信息
     *
     * @param channelType
     * @param isDown      是否是导出
     * @param isManager   是否是管理端
     * @return
     */
    public static Map<ExtendMothedEnum, List<String>> getExtendMap(String channelType, boolean isDown, boolean isManager) {
        Map<ExtendMothedEnum, List<String>> extendMap = Maps.newHashMap();
        for (ExtendEnum extendEnum : ExtendEnum.values()) {
            //如果channelType配置时为空则支持所有的渠道类型
            if ((StringUtils.isBlank(extendEnum.channelType) || extendEnum.channelType.equals(channelType))
                    //是否需要查询这个扩展字段
                    && (extendEnum.getQueryAndDown() || isDown)
                    //是否业务端查询这个扩展字段
                    && (extendEnum.getIsManagerAndBusiness() || isManager)) {
                //判断 之前有没有 该key
                List<String> extendList = extendMap.containsKey(extendEnum.extendMothedEnum) ? extendMap.get(extendEnum.extendMothedEnum) : Lists.newArrayList();
                extendList.add(extendEnum.fieldCode);
                //以查询表名为key， 以值集合为value
                extendMap.put(extendEnum.extendMothedEnum, extendList);
            }
        }
        return extendMap;
    }

    /**
     * 通过code及渠道类型查询枚举值
     *
     * @param channelType
     * @return
     */
    public static ExtendEnum getExtendEnum(String channelType, String fieldCode) {

        if (StringUtils.isBlank(channelType) || StringUtils.isBlank(fieldCode)) {
            return null;
        }
        for (ExtendEnum extendEnum : ExtendEnum.values()) {
            //如果channelType配置时为空则支持所有的渠道类型
            if (extendEnum.fieldCode.equals(fieldCode) &&
                    (StringUtils.isBlank(extendEnum.channelType) || extendEnum.channelType.equals(channelType))) {
                return extendEnum;
            }
        }
        return null;
    }

    /**
     * 设置扩展字段名
     *
     * @param insExtendVO
     * @param fieldValue
     */
    public abstract void setValue(InsExtendVO insExtendVO, String fieldValue);

    public String getChannelType() {
        return this.channelType;

    }

    public boolean getQueryAndDown() {
        return this.queryAndDown;

    }

    public boolean getIsManagerAndBusiness() {
        return this.isManagerAndBusiness;

    }

    public String getFieldCode() {
        return this.fieldCode;

    }

    public ExtendMothedEnum getExtendMothedEnum() {
        return this.extendMothedEnum;

    }

    public Boolean getIsAdd() {
        return this.isAdd;

    }

    public String getDesc() {
        return this.desc;

    }
}
